package com.woniu.cq.medical.service.impl;

import com.woniu.cq.medical.dao.mymapper.My_MedicalCardMapper;
import com.woniu.cq.medical.dao.mymapper.My_PatientMapper;
import com.woniu.cq.medical.entity.MedicalCard;
import com.woniu.cq.medical.dao.MedicalCardMapper;
import com.woniu.cq.medical.entity.Patient;
import com.woniu.cq.medical.service.IMedicalCardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2020-09-07
 */
@Service
public class MedicalCardServiceImpl extends ServiceImpl<MedicalCardMapper, MedicalCard> implements IMedicalCardService {

    @Autowired
    private My_PatientMapper patientMapper;
    @Autowired
    private My_MedicalCardMapper medicalCardMapper;
    @Override
    public Patient selectMessage(String card) {
        Patient patient = patientMapper.selectPatientByIdCard(card);
        return patient;
    }

    @Override
    @Transactional
    public void insertCard(int patientId) {

        MedicalCard medicalCard = new MedicalCard();
        medicalCard.setPatientId(patientId);
        medicalCard.setMedicalCardStatus("0");
        medicalCard.setMedicalCardIntoTime(new Date());
        medicalCard.setMedicalCardLastModificationTime(new Date());
        medicalCardMapper.insert(medicalCard);
        Integer medicalCardId = medicalCard.getMedicalCardId();
        Patient patient = new Patient();
        patient.setPatientId(patientId);
        patient.setMedicalCardId(medicalCardId);
        patient.setPatientLastModificationTime(new Date());
        patientMapper.updateById(patient);


    }


    @Override
    @Transactional
    public void updateCard(int patientId) {
        MedicalCard medicalCard1=new MedicalCard();
        medicalCard1.setPatientId(patientId);
        medicalCard1.setMedicalCardStatus("2");
        medicalCard1.setMedicalCardLastModificationTime(new Date());
        medicalCardMapper.updateCard(medicalCard1);
        MedicalCard medicalCard2=new MedicalCard();
        medicalCard2.setPatientId(patientId);
        medicalCard2.setMedicalCardStatus("0");
        medicalCard2.setMedicalCardIntoTime(new Date());
        medicalCard2.setMedicalCardLastModificationTime(new Date());
        medicalCardMapper.insert(medicalCard2);
        Integer medicalCardId = medicalCard2.getMedicalCardId();
        Patient patient=new Patient();
        patient.setPatientId(patientId);
        patient.setMedicalCardId(medicalCardId);
        patient.setPatientLastModificationTime(new Date());
        patientMapper.updateById(patient);

    }

    @Override
    @Transactional
    public void deleteCard(int patientId) {
        MedicalCard medicalCard1=new MedicalCard();
        medicalCard1.setPatientId(patientId);
        medicalCard1.setMedicalCardStatus("2");
        medicalCard1.setMedicalCardLastModificationTime(new Date());
        medicalCardMapper.updateCard(medicalCard1);
        Patient patient=new Patient();
        patient.setPatientId(patientId);
        patient.setMedicalCardId(-1);
        patient.setPatientLastModificationTime(new Date());
        patientMapper.updateById(patient);
    }
}
